package com.hugedata.cdn.service.task.action;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.zookeeper.common.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.hugedata.cdn.base.action.QueryBaseAction;
import com.hugedata.cdn.service.task.impl.TaskService;
import com.hugedata.cdn.util.CdnException;
import com.hugedata.cdn.util.ResultCode;

/**
 * 
 * 项目名称：xiaomifeng 类名称：TaskReportAction 类描述： 创建人：zhaoaihua 创建时间�?013-3-22
 * 上午11:23:21 修改备注�?
 * 
 * @version
 * 
 */
public class TaskReportPushAction extends QueryBaseAction
{
    private int ret;

    private String clientID;

    private String taskID;

    private int taskType;


    private TaskService taskReportService;

    // json数据校验工具
    // private XmfJSONValidator JsonValidator = new XmfJSONValidator(null);

    protected final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 
     * taskResultReport 节点上报结果处理接口
     * 
     * @return String DOM对象
     * @Exception 异常对象
     * @since CodingExample�?er(编码范例查看) 1.1
     */
    public String taskResultBatchReport()
    {
//        log.info("处理节点上报消息开始....");
//        try
//        {
//            // 从请求中抓取上报结果的json数据
//            String jsonString = fetchReportJsonData();
//            log.info("the req json str=" + jsonString);
//
//            // 格式化gson数据
//            Gson gson = new Gson();
//            TaskClientBatchReport batchbeans = gson.fromJson(jsonString,
//                    TaskClientBatchReport.class);
//
//            if (null == batchbeans)
//            {
//                ret = ResultCode.reportDataError;
//                log.error("resCode = " + ret
//                        + "----  report data is error----the req json data is="
//                        + jsonString);
//                log.error("the req info: [" + jsonString
//                        + "] is not right json format");
//                return "上报消息体格式错误....";
//            }
//
//            clientID = batchbeans.getClientID();
//
//            if (null == clientID)
//            {
//                ret = ResultCode.reportDataError;
//                log.error("resCode = " + ret
//                        + "----  report data is error\tclientID is null");
//                return "上报消息体没有节点ID错误，clientID:" + clientID;
//            }
//
//            ArrayList<ResultTask> datas = (ArrayList<ResultTask>) batchbeans
//                    .getData();
//
//            // 无上报
//            if (null == datas || (0 == datas.size()))
//            {
//                return "listbatch";
//            }
//            else
//            {
//                dealtDatasWithNode(datas, clientID);
//            }
//
//        }
//        catch (CdnException e)
//        {
//            ret = e.getErrorNum();
//            log.error("fetch json str exception", e);
//            return "errorlist";
//        }
//        catch (Exception e)
//        {
//            ret = ResultCode.SystemRunError;
//            log.error("resCode = " + ret
//                    + "---- client  report task result error,  clientID = "
//                    + clientID, e);
//            return "errorlist";
//        }
//        log.debug("resCode = " + ret
//                + "----client  report task result success,  clientID = "
//                + clientID);
        return "listbatch";
    }

    public int getRet()
    {
        return ret;
    }

    public void setRet(int ret)
    {
        this.ret = ret;
    }

    public String getClientID()
    {
        return clientID;
    }

    public void setClientID(String clientID)
    {
        this.clientID = clientID;
    }

    public String getTaskID()
    {
        return taskID;
    }

    public void setTaskID(String taskID)
    {
        this.taskID = taskID;
    }

    public int getTaskType()
    {
        return taskType;
    }

    public void setTaskType(int taskType)
    {
        this.taskType = taskType;
    }

    /**
     * 
     * fetchReportJsonData(这里用一句话描述这个方法的作�?
     * 
     * 获取请求体中的json数据
     * 
     * @return String
     * @throws XmfException
     * @Exception 异常对象
     */
    private String fetchReportJsonData() throws CdnException
    {

        StringBuffer sb = new StringBuffer();
       
        InputStreamReader reader = null;
        try
        {
            HttpServletRequest request = ServletActionContext.getRequest();
            
            request.setCharacterEncoding("UTF-8");
            ServletInputStream stream = request.getInputStream();  
           
            //GZIP解压
            final GZIPInputStream gzipInputStream = new GZIPInputStream(  
                    stream);  

            ServletInputStream newStream = new ServletInputStream() {  

                @Override  
                public int read() throws IOException {  
                    return gzipInputStream.read();  
                }  
            };  
  
            
            reader = new InputStreamReader(newStream, "UTF-8");
            char[] buff = new char[1024];
            int length = 0;
            while ((length = reader.read(buff)) != -1)
            {
                String json = new String(buff, 0, length);
                sb.append(json);
            }
            
        }
        catch (IOException e)
        {
            throw new CdnException(ResultCode.requestIoError, e);
        }
        finally
        {
            IOUtils.closeStream(reader);
        }
     
        return sb.toString();

    }

    public void setTaskReportService(TaskService taskReportService)
    {
        this.taskReportService = taskReportService;
    }
    
}
